package cn.pku.ss.fa.ui.component.friends;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.naming.InitialContext;

import org.zkforge.fckez.FCKeditor;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Html;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Vbox;
import org.zkoss.zul.Window;

import cn.pku.ss.fa.bean.BoardMessageBean;
import cn.pku.ss.fa.bean.PersonalBean;
import cn.pku.ss.fa.sessionbean.BoardManagerLocal;
import cn.pku.ss.fa.sessionbean.UserManagerLocal;
import cn.pku.ss.fa.ui.Common;
import cn.pku.ss.fa.ui.component.FriendsComponent;

public class NewMessageComponent extends Window{

	public void onCancel(){
		FCKeditor fck = (FCKeditor)getFellow("fck");
		fck.setValue("");
		this.setVisible(false);
	}
	
	public void onLeaveMessage(){
		Listbox messageList = (Listbox)getPage().getFellow("displayNotice").getFellow("messageList");
		FCKeditor fck = (FCKeditor)getFellow("fck");
		
		int fromUserId = (Integer)Executions.getCurrent().getDesktop().getSession().getAttribute(Common.USER_ID);
		int toUserId = FriendsComponent.getFriendIdInFriendPage();
		String messageContent = fck.getValue();
		BoardMessageBean message = new BoardMessageBean();
		message.setContent(messageContent);
		message.setFromUserId(fromUserId);
		message.setToUserId(toUserId);
		
		String fromUserRealName = "";
		String fromUserProfile = "";
		
		try {
			InitialContext ctx = new InitialContext();
			BoardManagerLocal boardManager = (BoardManagerLocal) ctx
					.lookup("FriendsAnywhere/BoardManager/local");
			UserManagerLocal userManager = (UserManagerLocal)ctx.lookup("FriendsAnywhere/UserManager/local");
			
			boardManager.leaveMessage(message);
			
			PersonalBean fromUser = userManager.findUserByUserId(fromUserId);
			fromUserRealName = fromUser.getRealName();
			fromUserProfile = fromUser.getProfile();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Listitem newCommentOrReply = newCommentOrReply(fck.getValue(),fromUserRealName, fromUserProfile);
		this.setVisible(false);
		fck.setValue("");
		messageList.appendChild(newCommentOrReply);
	}
	
	private Listitem newCommentOrReply(String content, String fromUserRealName, String fromUserProfile){
		String dateString = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		dateString = format.format(new Date());
		
		Listitem tmpItem = new Listitem();
		
		Listcell tmpImgCell = new Listcell();
		Image tmpImg = new Image();
		tmpImg.setSrc(fromUserProfile);
		tmpImg.setWidth("80px");
		tmpImg.setHeight("80px");
		tmpImgCell.appendChild(tmpImg);
		
		Listcell tmpContentCell = new Listcell();
		Vbox tmpVbox = new Vbox();
		Hbox tmpHbox = new Hbox();
		tmpHbox.setStyle("margin: 10px 0px;background: #F7F7F7");
		Label tmpLabel1 = new Label(fromUserRealName);
		tmpLabel1.setStyle("margin-right:200px");
		Label tmpLabel2 = new Label(dateString);
		tmpHbox.appendChild(tmpLabel1);
		tmpHbox.appendChild(tmpLabel2);
		Html tmpHtml = new Html();
		tmpHtml.setContent(content);
		tmpVbox.appendChild(tmpHbox);
		tmpVbox.appendChild(tmpHtml);
		tmpContentCell.appendChild(tmpVbox);
		
		Listcell tmpReply = new Listcell();
		
		tmpItem.appendChild(tmpImgCell);
		tmpItem.appendChild(tmpContentCell);
		tmpItem.appendChild(tmpReply);
		
		return tmpItem;
	}
}
